package components.contorls.superClass
{
	import components.core.IViewport;
	import components.core.UIComponent;
	import components.core.zn_internal;
	import components.events.ScrollBarEvent;
	import components.events.ViewPortMaxSizeChangeEvent;
	import components.utils.EventListenerUtil;
	
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	[Event(name="maxHeightChange", type="components.events.ViewPortMaxSizeChangeEvent")]
	[Event(name="change", type="flash.events.Event")]
	public class BaseTextArea extends UIComponent implements IViewport
	{
		private var _text:String = "TextArea";
		
		private var _scrollPolicy:String = "on";
		
		private var _editable:Boolean = true;
		
		private var _maxChars:int = 0;
		
		private var _isHtml:Boolean = false;
		
		private var _textFormat:TextFormat;
		
		private var _color:Number = 0x0;
		
		private var _oldMaxScrollV:int;
		
		private var _oldContentHeight:int;
		
		private var _autoScroll:Boolean = false;
		
		private var _showBorder:Boolean = false;

		private var _size:uint = 12;
		
		private var _bold:Boolean=false;
		
		public function BaseTextArea()
		{
			super();
			
			EventListenerUtil.addEventListener(textField,Event.SCROLL, tf_scrollHandler);
			EventListenerUtil.addEventListener(textField,Event.CHANGE, tf_changeHandler);
			
			EventListenerUtil.addEventListener(vScrollBar,ScrollBarEvent.UPDATE_PARM_COMPLETE, vScrollBar_updateParmComplete);
			vScrollBar.viewport = this;
		}
		
		/***************************************
		 *
		 * 显示对象声明
		 *
		 * **************************************/
		
		public function get vScrollBar():BaseVScrollBar
		{
			return getChildByName("vScrollBarSkin") as BaseVScrollBar;
		}
		
		public function get textField():TextField
		{
			return getChildByName("tfSkin") as TextField;
		}
		
		/***************************************
		 *
		 * 验证
		 *
		 * **************************************/
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			
			if (!_initialized)
			{
				_textFormat = textField.defaultTextFormat;
				setTFFilter();
				
				vScrollBar.top=0;
				vScrollBar.bottom=0;
				vScrollBar.right=0;
			}
			
			if (isHtml)
				textField.htmlText = text;
			else
				textField.text = text;
			
			textField.background = textField.border = showBorder;
		}
		
		protected override function measureSizes():Boolean
		{
			if (super.measureSizes())
			{
				textField.width = width - vScrollBar.width;
				textField.height = height;
				
				return true;
			}
			
			return false;
		}
		
		
		protected override function updateDisplayList():void
		{
			super.updateDisplayList();
			
			vScrollBar.scrollPolicy = scrollPolicy;
		}
		
		/***************************************
		 *
		 * 组件定义
		 *
		 * **************************************/
		
		[Inspectable(defaultValue="TextArea")]
		public function get text():String
		{
			return _text;
		}
		
		public function set text(value:String):void
		{
			if(value==null)
				_text="";
			else
				_text = value;
			invalidate();
		}
		
		[Inspectable(type="List", enumeration="auto,on,off", defaultValue="on")]
		public function get scrollPolicy():String
		{
			return _scrollPolicy;
		}
		
		public function set scrollPolicy(value:String):void
		{
			_scrollPolicy = value
			
			invalidate();
		}
		
		[Inspectable(defaultValue="true")]
		public function get editable():Boolean
		{
			return _editable;
		}
		
		public function set editable(value:Boolean):void
		{
			_editable = value;
			
			if (value)
				textField.type = TextFieldType.INPUT;
			else
				textField.type = TextFieldType.DYNAMIC;
		}
		
		[Inspectable(defaultValue="0")]
		public function get maxChars():int
		{
			return _maxChars;
		}
		
		public function set maxChars(value:int):void
		{
			_maxChars = value;
			textField.maxChars = value;
		}
		
		[Inspectable(defaultValue="false")]
		public function get isHtml():Boolean
		{
			return _isHtml;
		}
		
		public function set isHtml(value:Boolean):void
		{
			_isHtml = value;
			invalidate();
		}
		
		[Inspectable(defaultValue="#000000", type="Color")]
		public function get color():Number
		{
			return _color;
		}
		
		public function set color(value:Number):void
		{
			_color = value;
			if (_textFormat)
				_textFormat.color = value;
			invalidateTextFormate();
		}
		
		[Inspectable(defaultValue="false")]
		public function get autoScroll():Boolean
		{
			return _autoScroll;
		}
		
		public function set autoScroll(value:Boolean):void
		{
			_autoScroll = value;
			vScrollBar.autoScroll = value;
		}
		
		[Inspectable(defaultValue="false")]
		public function get showBorder():Boolean
		{
			return _showBorder;
		}
		
		public function set showBorder(value:Boolean):void
		{
			_showBorder = value;
			invalidate();
		}
		
		[Inspectable(defaultValue="false")]
		public function get bold():Boolean
		{
			return 	_bold;
		}
		
		public function set bold(value:Boolean):void
		{
			_bold = value;
			
			if (_textFormat)
				_textFormat.bold = value;
			
			invalidateTextFormate();
		}
		
		[Inspectable(defaultValue="12")]
		public function get size():uint
		{
			return _size;
		}
		
		public function set size(value:uint):void
		{
			_size = value;
			if (_textFormat)
				_textFormat.size = value;
			invalidateTextFormate();
		}
		
		/***************************************
		 *
		 * 其他代码
		 *
		 * **************************************/
		
		public function get contentHeight():Number
		{
			return textField.bottomScrollV - textField.scrollV + 1;
		}
		
		public function get contentWidth():Number
		{
			//TODO Auto-generated method stub
			return 0;
		}
		
		public function get horizontalScrollPosition():Number
		{
			//TODO Auto-generated method stub
			return 0;
		}
		
		public function set horizontalScrollPosition(value:Number):void
		{
			//TODO Auto-generated method stub
		}
		
		public function get maxHeight():Number
		{
			return textField.numLines;
		}
		
		public function get maxHorizontalScrollPosition():Number
		{
			//TODO Auto-generated method stub
			return 0;
		}
		
		public function get maxVerticalScrollPosition():Number
		{
			return textField.numLines - contentHeight;
		}
		
		public function get maxWidth():Number
		{
			//TODO Auto-generated method stub
			return 0;
		}
		
		public function get verticalScrollPosition():Number
		{
			return textField.scrollV - 1;
		}
		
		public function set verticalScrollPosition(value:Number):void
		{
			if (textField.scrollV == Math.round(value + 1))
				return;
			textField.scrollV = Math.round(value + 1);
		}
		
		protected function tf_scrollHandler(event:Event):void
		{
			vScrollBar.value = verticalScrollPosition * vScrollBar.zn_internal::stepScale;
			
			if (_oldMaxScrollV != textField.maxScrollV || _oldContentHeight != contentHeight)
			{
				dispatchScrollParm();
				_oldMaxScrollV = textField.maxScrollV;
				_oldContentHeight = contentHeight;
			}
		}
		
		protected function tf_changeHandler(event:Event):void
		{
			_text = textField.text;
			
			dispatchScrollParm();
			dispatchEvent(new Event(Event.CHANGE));
			_oldMaxScrollV = textField.maxScrollV;
		}
		
		protected function vScrollBar_updateParmComplete(event:Event):void
		{
			vScrollBar.step = vScrollBar.zn_internal::stepScale;
		}
		
		protected function dispatchScrollParm():void
		{
			var e:ViewPortMaxSizeChangeEvent = new ViewPortMaxSizeChangeEvent(ViewPortMaxSizeChangeEvent.MAX_HEIGHT_CHANGE);
			dispatchEvent(e);
		}
		
		public override function set focusComp(value:UIComponent):void
		{
			super.focusComp = value;
			vScrollBar.focusComp = focusComp;
		}
		
		public function invalidateTextFormate():void
		{
			textField.defaultTextFormat = _textFormat;
			textField.setTextFormat(_textFormat);
		}
		
		public override function destroy():void
		{
			super.destroy();
			
			_text = null;
			_scrollPolicy = null;
			_textFormat = null;
		}
		
		protected override function setTFFilter():void
		{
			if(tfFilterEnable)
				textField.filters = [ new GlowFilter(tfFilterColor, 1, 2, 2, 200)];
			else
				textField.filters=[];
		}
	}
}